package com.cloud.wyf.Util;

import com.cloud.wyf.Entity.Dto.LeaderShipDispartDto;
import com.cloud.wyf.Entity.Dto.LeaderShipResultDto;
import com.cloud.wyf.Entity.Dto.MiddelDispartDto;
import com.cloud.wyf.Entity.Dto.MiddelResultDto;
import org.apache.poi.hssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

public class DownloadUtil {
    public  static void exportExcel(HttpServletResponse response, String[] str, List<MiddelResultDto> data) {
        try {
            //实例化HSSFWorkbook
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个Excel表单，参数为sheet的名字
            HSSFSheet sheet = workbook.createSheet("sheet");
            //设置表头
            setTitle(workbook, sheet, str);
            //设置单元格并赋值
            setData(sheet, data);
            String filename=new String("1111.xls".getBytes(), StandardCharsets.UTF_8);
            //设置浏览器下载
            setBrowser(response, workbook,filename );
            System.out.println("导出解析成功!");
        } catch (Exception e) {
            System.out.println("导出解析失败!");
            e.printStackTrace();
        }
    }

    public  static void exportExcel1(HttpServletResponse response, String[] str, List<MiddelDispartDto> data) {
        try {
            //实例化HSSFWorkbook
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个Excel表单，参数为sheet的名字
            HSSFSheet sheet = workbook.createSheet("sheet");
            //设置表头
            setTitle(workbook, sheet, str);
            //设置单元格并赋值
            setData1(sheet, data);
            String filename=new String("1111.xls".getBytes(), StandardCharsets.UTF_8);
            //设置浏览器下载
            setBrowser(response, workbook,filename );
            System.out.println("导出解析成功!");
        } catch (Exception e) {
            System.out.println("导出解析失败!");
            e.printStackTrace();
        }
    }

    public  static void exportExcel2(HttpServletResponse response, String[] str, List<LeaderShipResultDto> data) {
        try {
            //实例化HSSFWorkbook
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个Excel表单，参数为sheet的名字
            HSSFSheet sheet = workbook.createSheet("sheet");
            //设置表头
            setTitle(workbook, sheet, str);
            //设置单元格并赋值
            setData2(sheet, data);
            String filename=new String("1111.xls".getBytes(), StandardCharsets.UTF_8);
            //设置浏览器下载
            setBrowser(response, workbook,filename );
            System.out.println("导出解析成功!");
        } catch (Exception e) {
            System.out.println("导出解析失败!");
            e.printStackTrace();
        }
    }

    public  static void exportExcel3(HttpServletResponse response, String[] str, List<LeaderShipDispartDto> data) {
        try {
            //实例化HSSFWorkbook
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个Excel表单，参数为sheet的名字
            HSSFSheet sheet = workbook.createSheet("sheet");
            //设置表头
            setTitle(workbook, sheet, str);
            //设置单元格并赋值
            setData3(sheet, data);
            String filename=new String("1111.xls".getBytes(), StandardCharsets.UTF_8);
            //设置浏览器下载
            setBrowser(response, workbook,filename );
            System.out.println("导出解析成功!");
        } catch (Exception e) {
            System.out.println("导出解析失败!");
            e.printStackTrace();
        }
    }

    /**
     * setTitle设置标题
     * */
    private static void setTitle(HSSFWorkbook workbook, HSSFSheet sheet, String[] str) {
        try {
            HSSFRow row = sheet.createRow(0);
            //设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度
            for (int i = 0; i <= str.length; i++) {
                sheet.setColumnWidth(i, 15 * 256);
            }
            //设置为居中加粗,格式化时间格式
            HSSFCellStyle style = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setBold(true);
            style.setFont(font);
            style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
            //创建表头名称
            HSSFCell cell;
            for (int j = 0; j < str.length; j++) {
                cell = row.createCell(j);
                cell.setCellValue(str[j]);
                cell.setCellStyle(style);
            }
        } catch (Exception e) {
            System.out.println("导出时设置表头失败！");
            e.printStackTrace();
        }
    }

    /**
     * 方法名：setData
     * 功能：表格赋值
     */
    private static void setData(HSSFSheet sheet, List<MiddelResultDto> data) {
        try{
            int rowNum = 1;
            int num=1;
            for (int i = 0; i < data.size(); i++) {
                HSSFRow row = sheet.createRow(rowNum);
                for (int j = 0; j < 8; j++) {
                    if (j == 0) row.createCell(j).setCellValue(num);
                    else if (j == 1) row.createCell(j).setCellValue(data.get(i).getName());
                    else if (j == 2) row.createCell(j).setCellValue(data.get(i).getPart());
                    else if (j == 3) row.createCell(j).setCellValue(data.get(i).getLscore());
                    else if (j == 4) {
                        if(data.get(i).getLscore()>=86)
                            row.createCell(j).setCellValue("优秀");
                        else if(data.get(i).getLscore()>=70&&data.get(i).getLscore()<86)row.createCell(j).setCellValue("称职");
                        else if(data.get(i).getLscore()>=60&&data.get(i).getLscore()<70)row.createCell(j).setCellValue("基本称职");
                        else if(data.get(i).getLscore()<60)row.createCell(j).setCellValue("不称职");
                    } else if (j == 5) {
                        row.createCell(j).setCellValue(data.get(i).getOscore());
                    } else if (j == 6) {
                        if(data.get(i).getOscore()>=86)
                            row.createCell(j).setCellValue("优秀");
                        else if(data.get(i).getOscore()>=70&&data.get(i).getOscore()<86)row.createCell(j).setCellValue("称职");
                        else if(data.get(i).getOscore()>=60&&data.get(i).getOscore()<70)row.createCell(j).setCellValue("基本称职");
                        else if(data.get(i).getOscore()<60)row.createCell(j).setCellValue("不称职");
                    } else if (j == 7) {
                            row.createCell(j).setCellValue(data.get(i).getTotal());
                    }
                }
                rowNum++;
                num++;
            }
            System.out.println("表格赋值成功！");
        }catch (Exception e){
            System.out.println("表格赋值失败！");
            e.printStackTrace();
        }
    }

    private static void setData1(HSSFSheet sheet, List<MiddelDispartDto> data) {
        try{
            int rowNum = 1;
            int num=1;
            for (int i = 0; i < data.size(); i++) {
                HSSFRow row = sheet.createRow(rowNum);
                for (int j = 0; j < 15; j++) {
                    if (j == 0) row.createCell(j).setCellValue(num);
                    else if (j == 1) row.createCell(j).setCellValue(data.get(i).getName());
                    else if (j == 2) row.createCell(j).setCellValue(data.get(i).getPart());
                    else if (j == 3) row.createCell(j).setCellValue(data.get(i).getLitem1());
                    else if (j == 4) row.createCell(j).setCellValue(data.get(i).getLitem2());
                    else if (j == 5) row.createCell(j).setCellValue(data.get(i).getLitem3());
                    else if (j == 6) row.createCell(j).setCellValue(data.get(i).getLitem4());
                    else if (j == 7) row.createCell(j).setCellValue(data.get(i).getLitem5());
                    else if (j == 8) row.createCell(j).setCellValue(data.get(i).getLtotal());
                    else if (j == 9) row.createCell(j).setCellValue(data.get(i).getOitem1());
                    else if (j == 10) row.createCell(j).setCellValue(data.get(i).getOitem2());
                    else if (j == 11) row.createCell(j).setCellValue(data.get(i).getOitem3());
                    else if (j == 12) row.createCell(j).setCellValue(data.get(i).getOitem4());
                    else if (j == 13) row.createCell(j).setCellValue(data.get(i).getOitem5());
                    else if (j == 14) row.createCell(j).setCellValue(data.get(i).getOtotal());
                }
                rowNum++;
                num++;
            }
            System.out.println("表格赋值成功！");
        }catch (Exception e){
            System.out.println("表格赋值失败！");
            e.printStackTrace();
        }
    }

    private static void setData2(HSSFSheet sheet, List<LeaderShipResultDto> data) {
        try{
            int rowNum = 1;
            int num=1;
            for (int i = 0; i < data.size(); i++) {
                HSSFRow row = sheet.createRow(rowNum);
                for (int j = 0; j < 7; j++) {
                    if (j == 0) row.createCell(j).setCellValue(num);
                    else if (j == 1) row.createCell(j).setCellValue(data.get(i).getName());
                    else if (j == 2) row.createCell(j).setCellValue(data.get(i).getLscore());
                    else if (j == 3) {
                        if(data.get(i).getLscore()>=86)
                            row.createCell(j).setCellValue("优秀");
                        else if(data.get(i).getLscore()>=70&&data.get(i).getLscore()<86)row.createCell(j).setCellValue("称职");
                        else if(data.get(i).getLscore()>=60&&data.get(i).getLscore()<70)row.createCell(j).setCellValue("基本称职");
                        else if(data.get(i).getLscore()<60)row.createCell(j).setCellValue("不称职");
                    } else if (j == 4) {
                        row.createCell(j).setCellValue(data.get(i).getOscore());
                    } else if (j == 5) {
                        if(data.get(i).getOscore()>=86)
                            row.createCell(j).setCellValue("优秀");
                        else if(data.get(i).getOscore()>=70&&data.get(i).getOscore()<86)row.createCell(j).setCellValue("称职");
                        else if(data.get(i).getOscore()>=60&&data.get(i).getOscore()<70)row.createCell(j).setCellValue("基本称职");
                        else if(data.get(i).getOscore()<60)row.createCell(j).setCellValue("不称职");
                    } else if (j == 6) {
                        row.createCell(j).setCellValue(data.get(i).getTotal());
                    }
                }
                rowNum++;
                num++;
            }
            System.out.println("表格赋值成功！");
        }catch (Exception e){
            System.out.println("表格赋值失败！");
            e.printStackTrace();
        }
    }

    private static void setData3(HSSFSheet sheet, List<LeaderShipDispartDto> data) {
        try{
            int rowNum = 1;
            int num=1;
            for (int i = 0; i < data.size(); i++) {
                HSSFRow row = sheet.createRow(rowNum);
                for (int j = 0; j < 14; j++) {
                    if (j == 0) row.createCell(j).setCellValue(num);
                    else if (j == 1) row.createCell(j).setCellValue(data.get(i).getName());
                    else if (j == 2) row.createCell(j).setCellValue(data.get(i).getLitem1());
                    else if (j == 3) row.createCell(j).setCellValue(data.get(i).getLitem2());
                    else if (j == 4) row.createCell(j).setCellValue(data.get(i).getLitem3());
                    else if (j == 5) row.createCell(j).setCellValue(data.get(i).getLitem4());
                    else if (j == 6) row.createCell(j).setCellValue(data.get(i).getLitem5());
                    else if (j == 7) row.createCell(j).setCellValue(data.get(i).getLtotal());
                    else if (j == 8) row.createCell(j).setCellValue(data.get(i).getOitem1());
                    else if (j == 9) row.createCell(j).setCellValue(data.get(i).getOitem2());
                    else if (j == 10) row.createCell(j).setCellValue(data.get(i).getOitem3());
                    else if (j == 11) row.createCell(j).setCellValue(data.get(i).getOitem4());
                    else if (j == 12) row.createCell(j).setCellValue(data.get(i).getOitem5());
                    else if (j == 13) row.createCell(j).setCellValue(data.get(i).getOtotal());
                }
                rowNum++;
                num++;
            }
            System.out.println("表格赋值成功！");
        }catch (Exception e){
            System.out.println("表格赋值失败！");
            e.printStackTrace();
        }
    }
    /**
     * 方法名：setBrowser
     * 功能：使用浏览器下载
     */
    private static void setBrowser(HttpServletResponse response, HSSFWorkbook workbook, String fileName) {
        try {
            //清空response
            //response.reset();
            //设置response的Header
           // response.addHeader("Access-Control-Allow-Origin","*");
            response.setCharacterEncoding("utf8");
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            OutputStream os = new BufferedOutputStream(response.getOutputStream());
            //将excel写入到输出流中
            workbook.write(os);
            os.flush();
            os.close();
            System.out.println("设置浏览器下载成功！");
        } catch (Exception e) {
            System.out.println("设置浏览器下载失败！");
            e.printStackTrace();
        }

    }
}
